package com.sunda.spmswms.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmswms.entity.WhsOtherDtl;
import com.sunda.spmswms.mapper.WhsOtherDtlMapper;
import com.sunda.spmswms.service.IWhsOtherDtlService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * <p>
 * 异常出入库明细表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-06-25
 */
@Service
public class WhsOtherDtlServiceImpl extends ServiceImpl<WhsOtherDtlMapper, WhsOtherDtl> implements IWhsOtherDtlService {


    @Override
    public ResponseResult getWhsOtherDtl(String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            return ResponseResult.error("uuid is mandatory required");
        }
        return ResponseResult.success().add("whsOtherDtl", this.baseMapper.selectWhsOtherDtl(uuid));
    }

    /**
     * 更新或创建WhsOther Dtl
     * 以uuid及rowId为key，存在则更新，不存在则新建
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateWhsOtherDtl(String uuid, JSONArray whsOtherDtls) {
        try {
            if (StringUtils.isEmpty(uuid) || null == whsOtherDtls || whsOtherDtls.size() <= 0) {
                throw new Exception("uuid and whsOtherDtls are mandatory required");
            }

            int rowId = this.baseMapper.selectMaxRowId(uuid);
            for (int i = 0; i < whsOtherDtls.size(); i++) {
                JSONObject dtl = whsOtherDtls.getJSONObject(i);
                WhsOtherDtl whsOtherDtl = dtl.toJavaObject(WhsOtherDtl.class);
                whsOtherDtl.setTempStorage(dtl.containsKey("tempStorage") ? dtl.getJSONArray("tempStorage").toJSONString() : "[]");
                whsOtherDtl.setStorageIn(dtl.containsKey("storageIn") ? dtl.getJSONArray("storageIn").toJSONString() : "[]");
                whsOtherDtl.setStorageOut(dtl.containsKey("storageOut") ? dtl.getJSONArray("storageOut").toJSONString() : "[]");
                QueryWrapper<WhsOtherDtl> whsDtlQuerywarp = new QueryWrapper<WhsOtherDtl>().eq("WHS_OTHER_UUID", uuid).eq("ROW_ID", whsOtherDtl.getRowId());
                List<WhsOtherDtl> whsDtls = this.baseMapper.selectList(whsDtlQuerywarp);
                if (whsDtls.size() > 0) {
                    // 存在则更新
                    this.baseMapper.update(whsOtherDtl, whsDtlQuerywarp);
                } else {
                    // 不存在，新建
                    rowId += 10;
                    whsOtherDtl.setRowId(Integer.valueOf(rowId));
                    whsOtherDtl.setWhsOtherUuid(uuid);
                    this.baseMapper.insert(whsOtherDtl);
                }
            }
            return ResponseResult.success().add("whsOtherDtl", this.baseMapper.selectWhsOtherDtl(uuid));
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.error(e.getMessage());
        }
    }

    @Override
    public List<WhsOtherDtl> getWhsOtherDtlList(String uuid) {
        if (StringUtils.isEmpty(uuid)){
            return null;
        }
        return this.baseMapper.selectList(new QueryWrapper<WhsOtherDtl>().eq("WHS_OTHER_UUID", uuid));
    }
}
